package homework712;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

//1：读取products.xml中的商品信息，输出。
//itemno为商品编号，name为商品名称，price为商品单价，unit是计价单位，stockcount是库存量
public class work1 {
	public static void main(String[] args) {
		SAXReader reader = new SAXReader();
		Document doc;
		try {
			doc = reader.read(new File("src/homework712/products.xml"));
			Element root = doc.getRootElement();
			List<Element> list = root.selectNodes("product");
			for (Element e : list) {
				String discount = e.attributeValue("discount");
				String factoryaddr = e.attributeValue("factoryaddr");
				if (factoryaddr != null) {
				} else {
					factoryaddr = "未知产地";
				}
				String itemno = e.selectSingleNode("itemno").getText();
				int i = Integer.parseInt(itemno);
				Element name = (Element) e.selectSingleNode("name");
				String n = name.getText();
				String price = e.selectSingleNode("price").getText();
				double p = Double.parseDouble(price);
				Element unit = (Element) e.selectSingleNode("unit");
				String u = unit.getText();
				String stockcount = e.selectSingleNode("stockcount").getText();
				int s = Integer.parseInt(stockcount);
				System.out.println("商品编号：" + i + " 名称：" + n + "产自" + factoryaddr + " 单价：" + p + " 现在还有" + s + u
						+ "目前打折状态：" + discount);
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			}

		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}

}
